package com.jhuster.jnote;

        import android.app.ProgressDialog;
        import android.content.Intent;
        import android.os.Bundle;
        import android.os.Looper;
        import android.support.v7.app.AppCompatActivity;
        import android.util.Log;
        import android.view.View;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.TextView;
        import android.widget.Toast;

        import java.io.DataInputStream;
        import java.io.DataOutputStream;
        import java.net.InetSocketAddress;
        import java.net.Socket;
        import java.net.SocketAddress;



public class ChangePasswordActivity extends AppCompatActivity {
    private static final String TAG = "ChangePasswordActivity";



    private EditText _emailText;

    private EditText _oldpasswordText;
    private EditText _passwordText;
    private EditText _reEnterPasswordText;

    private Button _changepasswordButton;
    private TextView _loginLink;





    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);


        _emailText=(EditText) findViewById(R.id.input_email);
        _oldpasswordText=(EditText) findViewById(R.id.input_oldpassword);
        _passwordText=(EditText) findViewById(R.id.input_password);
        _reEnterPasswordText=(EditText) findViewById(R.id.input_reEnterPassword);
        _changepasswordButton=(Button) findViewById(R.id.btn_change);
        _loginLink=(TextView) findViewById(R.id.link_login);






        _changepasswordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isFastClick()) {
                    // 进行点击事件后的逻辑操作
                    changepassword();
                }

            }
        });

        _loginLink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Finish the registration screen and return to the Login activity
                Intent intent = new Intent(getApplicationContext(),LoginActivity.class);
                startActivity(intent);
                finish();
                overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
            }
        });
    }





    public void changepassword() {
        Log.d(TAG, "Changepassword");

        if (!validate()) {
            onChangepasswordFailed();
            return;
        }

        _changepasswordButton.setEnabled(false);

        final ProgressDialog progressDialog = new ProgressDialog(ChangePasswordActivity.this,
                R.style.AppTheme_Dark_Dialog);
        progressDialog.setIndeterminate(true);
        progressDialog.setMessage("Changing Password...");
        progressDialog.show();


        String email = _emailText.getText().toString();
        String oldpassword = _oldpasswordText.getText().toString();
        String password = _passwordText.getText().toString();
        String reEnterPassword = _reEnterPasswordText.getText().toString();

        // TODO: Implement your own change password logic here.

        new android.os.Handler().postDelayed(
                new Runnable() {
                    public void run() {
                        // On complete call either onSignupSuccess or onSignupFailed
                        // depending on success
                        onChangepasswordSuccess();
                        // onSignupFailed();
                        progressDialog.dismiss();
                    }
                }, 2000);
    }


    public void onChangepasswordSuccess() {
        _changepasswordButton.setEnabled(true);
        Intent intent = new Intent(this,LoginActivity.class);
        startActivity(intent);
        finish();
    }

    public void onChangepasswordFailed() {
        Toast.makeText(getBaseContext(), "Change password failed", Toast.LENGTH_LONG).show();

        _changepasswordButton.setEnabled(true);
    }



    private Thread newThread;         //声明一个子线程
    static Socket client;
    public static volatile boolean valid=true;

    public boolean validate() {
        valid = true;

        String email = _emailText.getText().toString();
        String oldpassword = _oldpasswordText.getText().toString();
        String password = _passwordText.getText().toString();
        String reEnterPassword = _reEnterPasswordText.getText().toString();


        if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            _emailText.setError("enter a valid email address");
            valid = false;
        } else {
            _emailText.setError(null);
        }

        if (oldpassword.isEmpty() || oldpassword.length() < 4 || oldpassword.length() > 10) {
            _oldpasswordText.setError("between 4 and 10 alphanumeric characters");
            valid = false;
        } else {
            _oldpasswordText.setError(null);
        }

        if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
            _passwordText.setError("between 4 and 10 alphanumeric characters");
            valid = false;
        } else {
            _passwordText.setError(null);
        }

        if (reEnterPassword.isEmpty() || reEnterPassword.length() < 4 || reEnterPassword.length() > 10 || !(reEnterPassword.equals(password))) {
            _reEnterPasswordText.setError("Password Do not match");
            valid = false;
        } else {
            _reEnterPasswordText.setError(null);
        }


        if(email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()||oldpassword.isEmpty() || oldpassword.length() < 4 || oldpassword.length() > 10||password.isEmpty() || password.length() < 4 || password.length() > 10||reEnterPassword.isEmpty() || reEnterPassword.length() < 4 || reEnterPassword.length() > 10 || !(reEnterPassword.equals(password))){

        }else{
            newThread=new Thread() {
                @Override
                public void run() {
                    //这里写入子线程需要做的工作
                    String result="";
                    String email = _emailText.getText().toString();
                    String oldpassword = _oldpasswordText.getText().toString();
                    String password = _passwordText.getText().toString();
                    try {

                        //client = new Socket("e1868x8452.iok.la", 22329);
                        client = new Socket();
                        SocketAddress socketAddress = new InetSocketAddress("e1868x8452.iok.la", 22329);
                        client.connect(socketAddress,3000);//单位为毫秒




                        DataOutputStream dos = new DataOutputStream(client.getOutputStream());
                        DataInputStream dis = new DataInputStream(client.getInputStream());
                        dos.writeUTF("changepassword");
                        dos.flush();
                        dos.writeUTF(email +" "+ oldpassword + " " + password);
                        dos.flush();
                        result = dis.readUTF();
                        if (dos != null)
                            dos.close();
                        if (result != null) {
                            switch (result) {
                                case "changepassword":
                                    //formula.setText("login");
                                    //Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
                                    //startActivityForResult(intent, 0);
                                    //finish();
                                    //overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
                                    _emailText.setError(null);
                                    _oldpasswordText.setError(null);
                                    _passwordText.setError(null);
                                    _reEnterPasswordText.setError(null);
                                    break;
                                case "passwordiswrong":
                                    _oldpasswordText.setError("password is wrong");
                                    valid = false;
                                    break;
                                case "emailnotregist":
                                    _emailText.setError("email address is not regist");
                                    valid = false;
                                    break;
                                default:
                                    valid = false;
                                    break;
                            }
                        }else{

                            Toast.makeText(getBaseContext(), "Lost connection!", Toast.LENGTH_LONG).show();

                        }
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        //e.printStackTrace();
                        valid=false;
                        //_emailText.setError("连接失败");
                        //Looper.prepare();
                        //Toast.makeText(getBaseContext(), "Lost connection!", Toast.LENGTH_LONG).show();
                        //Looper.loop();   //会卡死回不到主线程

                    }
                }
            };
            newThread.start();
            try {
                newThread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }//如果子线程没执行完毕数据不会改变
        }




        return valid;
    }
}